/* Y o u r   D e s c r i p t i o n                       */
/*                            AppBuilder Photon Code Lib */
/*                                         Version 2.01  */

/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/* Local headers */
#include "ablibs.h"
#include "abimport.h"
#include "proto.h"
#include "parse_alarms.h"
#include "alarm_table.h"
#include "trenddb.h"

char *ep1_addr [5] = {"Mentor_14_0502", "Mentor_13_0502", "Mentor_14_0304", "Mentor_14_0303", "Mentor_14_0117"};
char *ep2_addr [5] = {"Mentor_24_0502", "Mentor_23_0502", "Mentor_24_0304", "Mentor_24_0303", "Mentor_24_0117"};

alarms_t *alarms;


int
setup_all( PtWidget_t *link_instance, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	zone_t zones [5];
	double max, min;
	char str [255];
	int i;
	alarm_item_t *alarm_item, *alarm_item_prv;

	/* eliminate 'unreferenced' warnings */
	link_instance = link_instance, apinfo = apinfo, cbinfo = cbinfo;


	//-------------------------------------------------------------------------	
	trenddb_init_ip ("/trenddb", &myself, &hist);	
	//-------------------------------------------------------------------------	
	min = 0;
	PtSetResource (ABW_CwGauge_Ia1, GAUGE_ARG_MIN, &min, 0);
	PtSetResource (ABW_CwGauge_Ia2, GAUGE_ARG_MIN, &min, 0);
	max = 3.5;
	PtSetResource (ABW_CwGauge_Ia1, GAUGE_ARG_MAX, &max, 0);
	PtSetResource (ABW_CwGauge_Ia2, GAUGE_ARG_MAX, &max, 0);
	strcpy (str, "%.1f");
	PtSetResource (ABW_CwGauge_Ia1, GAUGE_ARG_STRFMT, str, 0);
	PtSetResource (ABW_CwGauge_Ia2, GAUGE_ARG_STRFMT, str, 0);
	PtSetResource (ABW_CwGauge_Ia1, GAUGE_ARG_NDIV, 7, 0);
	PtSetResource (ABW_CwGauge_Ia2, GAUGE_ARG_NDIV, 7, 0);
	PtSetResource (ABW_CwGauge_Ia1, GAUGE_ARG_NSUBDIV, 2, 0);
	PtSetResource (ABW_CwGauge_Ia2, GAUGE_ARG_NSUBDIV, 2, 0);
	zones [0].min = 0;
	zones [0].max = 2.5;
	zones [0].color = Pg_GREEN;	
	zones [1].min = 2.5;
	zones [1].max = 3;
	zones [1].color = Pg_YELLOW;	
	zones [2].min = 3;
	zones [2].max = 3.6;
	zones [2].color = Pg_RED;	
	PtSetResource (ABW_CwGauge_Ia1, GAUGE_ARG_ZONES, zones, 3);
	PtSetResource (ABW_CwGauge_Ia2, GAUGE_ARG_ZONES, zones, 3);
	//-------------------------------------------------------------------------
	min = 0;
	PtSetResource (ABW_CwGauge_Iv1, GAUGE_ARG_MIN, &min, 0);
	PtSetResource (ABW_CwGauge_Iv2, GAUGE_ARG_MIN, &min, 0);
	max = 120;
	PtSetResource (ABW_CwGauge_Iv1, GAUGE_ARG_MAX, &max, 0);
	PtSetResource (ABW_CwGauge_Iv2, GAUGE_ARG_MAX, &max, 0);
	strcpy (str, "%.0f");
	PtSetResource (ABW_CwGauge_Iv1, GAUGE_ARG_STRFMT, str, 0);
	PtSetResource (ABW_CwGauge_Iv2, GAUGE_ARG_STRFMT, str, 0);
	PtSetResource (ABW_CwGauge_Iv1, GAUGE_ARG_NDIV, 6, 0);
	PtSetResource (ABW_CwGauge_Iv2, GAUGE_ARG_NDIV, 6, 0);
	PtSetResource (ABW_CwGauge_Iv1, GAUGE_ARG_NSUBDIV, 4, 0);
	PtSetResource (ABW_CwGauge_Iv2, GAUGE_ARG_NSUBDIV, 4, 0);
	zones [0].min = 0;
	zones [0].max = 34;
	zones [0].color = Pg_RED;	
	zones [1].min = 34;
	zones [1].max = 105;
	zones [1].color = Pg_GREEN;	
	zones [2].min = 105;
	zones [2].max = 116;
	zones [2].color = Pg_RED;	
	PtSetResource (ABW_CwGauge_Iv1, GAUGE_ARG_ZONES, zones, 3);
	PtSetResource (ABW_CwGauge_Iv2, GAUGE_ARG_ZONES, zones, 3);
	//-------------------------------------------------------------------------
	min = 0;
	PtSetResource (ABW_CwGauge_Ua1, GAUGE_ARG_MIN, &min, 0);
	PtSetResource (ABW_CwGauge_Ua2, GAUGE_ARG_MIN, &min, 0);
	max = 900;
	PtSetResource (ABW_CwGauge_Ua1, GAUGE_ARG_MAX, &max, 0);
	PtSetResource (ABW_CwGauge_Ua2, GAUGE_ARG_MAX, &max, 0);
	strcpy (str, "%.0f");
	PtSetResource (ABW_CwGauge_Ua1, GAUGE_ARG_STRFMT, str, 0);
	PtSetResource (ABW_CwGauge_Ua2, GAUGE_ARG_STRFMT, str, 0);
	PtSetResource (ABW_CwGauge_Ua1, GAUGE_ARG_NDIV, 9, 0);
	PtSetResource (ABW_CwGauge_Ua2, GAUGE_ARG_NDIV, 9, 0);
	PtSetResource (ABW_CwGauge_Ua1, GAUGE_ARG_NSUBDIV, 2, 0);
	PtSetResource (ABW_CwGauge_Ua2, GAUGE_ARG_NSUBDIV, 2, 0);
	zones [0].min = 0;
	zones [0].max = 750;
	zones [0].color = Pg_GREEN;	
	zones [1].min = 750;
	zones [1].max = 900;
	zones [1].color = Pg_RED;	
	PtSetResource (ABW_CwGauge_Ua1, GAUGE_ARG_ZONES, zones, 2);
	PtSetResource (ABW_CwGauge_Ua2, GAUGE_ARG_ZONES, zones, 2);
	//-------------------------------------------------------------------------
	min = 0;
	PtSetResource (ABW_CwGauge_n1, GAUGE_ARG_MIN, &min, 0);
	PtSetResource (ABW_CwGauge_n2, GAUGE_ARG_MIN, &min, 0);
	max = 250;
	PtSetResource (ABW_CwGauge_n1, GAUGE_ARG_MAX, &max, 0);
	PtSetResource (ABW_CwGauge_n2, GAUGE_ARG_MAX, &max, 0);
	strcpy (str, "%.0f");
	PtSetResource (ABW_CwGauge_n1, GAUGE_ARG_STRFMT, str, 0);
	PtSetResource (ABW_CwGauge_n2, GAUGE_ARG_STRFMT, str, 0);
	PtSetResource (ABW_CwGauge_n1, GAUGE_ARG_NDIV, 10, 0);
	PtSetResource (ABW_CwGauge_n2, GAUGE_ARG_NDIV, 10, 0);
	PtSetResource (ABW_CwGauge_n1, GAUGE_ARG_NSUBDIV, 2, 0);
	PtSetResource (ABW_CwGauge_n2, GAUGE_ARG_NSUBDIV, 2, 0);
	zones [0].min = 0;
	zones [0].max = 125;
	zones [0].color = Pg_GREEN;	
	zones [1].min = 125;
	zones [1].max = 250;
	zones [1].color = Pg_YELLOW;	
	PtSetResource (ABW_CwGauge_n1, GAUGE_ARG_ZONES, zones, 2);
	PtSetResource (ABW_CwGauge_n2, GAUGE_ARG_ZONES, zones, 2);
	//-------------------------------------------------------------------------

	PtSetResource (ABW_CwTrend1, TREND_ARG_HIST_NAME, ep1_addr, 5);
	PtSetResource (ABW_CwTrend2, TREND_ARG_HIST_NAME, ep2_addr, 5);
	//-------------------------------------------------------------------------
	
	PtSetResource (ABW_CwLadder1, Cw_LADDER_LABEL, ABW_ladder_label, 0);
	PtSetResource (ABW_CwLadder1, Cw_LADDER_LIST, ABW_ladder_list, 0);
	//-------------------------------------------------------------------------
		
	alarms = parse_alarms ("/proj4/cfg/alarms.xml");	
	PtGenListRemoveItems (ABW_ActiveAlarmsList, NULL, NULL);
	alarm_item_prv = NULL;
	for (i= 0; i< alarms->n_alarm; i++) {
		if (alarms->alarm [i].priority >= 1) {
			char addr [255];
			alarm_item = malloc (sizeof (alarm_item_t));
			memset (alarm_item, 0, sizeof (alarm_item_t));
			alarm_item->item.size.h = 18;
			alarm_item->rec.priority = alarms->alarm [i].priority;
			alarm_item->rec.msg = strdup (alarms->alarm [i].msg);
			sprintf (addr, "CJ1M_%s_%06d", alarms->alarm [i].name, alarms->alarm [i].addr);
			alarm_item->rec.addr = strdup (addr);
			alarm_item->rec.bitnum = alarms->alarm [i].bitnum;
			if (alarms->alarm [i].comment_id < alarms->n_comment) {
				alarm_item->rec.comments = strdup (alarms->comment [alarms->alarm [i].comment_id].txt);
			}
			PtGenListAddItems (ABW_ActiveAlarmsList, &alarm_item->item, &alarm_item_prv->item);
			alarm_item_prv = alarm_item;
		}			
	}
	PtSetResource (ABW_ActiveAlarmsList, Pt_ARG_TOP_ITEM_POS, 1, 0);
	//-------------------------------------------------------------------------

	PtSetResource (ABW_PtPanelGroup1, Pt_ARG_PG_CURRENT_INDEX, 0, 0);
	PtSetResource (ABW_PtPanelGroup2, Pt_ARG_PG_CURRENT_INDEX, 0, 0);
	PtSetResource (ABW_PtPanelGroup3, Pt_ARG_PG_CURRENT_INDEX, 0, 0);

	return( Pt_CONTINUE );
}

